﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WFSample
{
    public class PlatformWindowsEventArgs : EventArgs
    {
        public Form Window { get; private set; }

        public PlatformWindowsEventArgs(Form w)
        {
            this.Window = w;
        }
    }

    public class App
    {
        public event EventHandler<PlatformWindowsEventArgs> Opening;

        public DialogResult ShowDialog(Control content, Action<Form> windowSetter = null)
        {
            Form form = CreateWindow(content);
            windowSetter?.Invoke(form);

            this.OnOpening(form);
            return form.ShowDialog();
        }

        private Form CreateWindow(Control view)
        {
            if (view == null)
                throw new ArgumentNullException(nameof(view));
            Form from = new Form();

            from.Controls.Add(view);
            return from;
        }

        protected void OnOpening(Form w)
        {
            if (this.Opening == null)
                return;
            this.Opening(this, new PlatformWindowsEventArgs(w));
        }
    }
}
